import cv2
from utils.log_manager import my_logger


class ImageRecognitionManager:

    def campare_pic(self, main_img_path, sub_img_path):
        # 读取主图和子图
        main_img = cv2.imread(main_img_path)
        sub_img = cv2.imread(sub_img_path)

        # 获取主图和子图的高度、宽度和通道数
        main_height, main_width, _ = main_img.shape
        sub_height, sub_width, _ = sub_img.shape

        # 在主图上查找子图位置
        result = cv2.matchTemplate(main_img, sub_img, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, _, (x, y) = cv2.minMaxLoc(result)

        if max_val >= 0.85 and x < main_width - sub_width and y < main_height - sub_height:
            my_logger.logger.info('图片对比成功，已经在目标页面上匹配成功')
            return True
        else:
            return False


img_manager = ImageRecognitionManager()
